package cn.jinhe.doc.view.action;

import java.text.DecimalFormat;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.jinhe.doc.base.BaseAction;
import cn.jinhe.doc.domain.Plan;
import cn.jinhe.doc.domain.Step;

@Controller
@Scope("prototype")
public class StepAction extends BaseAction<Step>{
	private Long id;
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	private Long planId;
	public String delete() throws Exception {
		Plan plan=planService.getById(planId);
		Step step=stepService.getById(model.getId());
		if(step.getIsFinished()==0){
			
		plan.setUnfinished(plan.getUnfinished()-1);
		
		}
		if(step.getIsFinished()==1){
		plan.setFinished(plan.getFinished()-1);
			
		}
		Double process= (double)plan.getFinished()/(double)(plan.getFinished()+plan.getUnfinished());		
		plan.setProcess( (int) (process*100));
		
		stepService.delete(model.getId());
		return "toPlanShow";	

	}
	
	public String finish() throws Exception {
		Plan plan=planService.getById(planId);
		plan.setFinished(plan.getFinished()+1);
		plan.setUnfinished(plan.getUnfinished()-1);
		
		Double process= (double)plan.getFinished()/(double)(plan.getFinished()+plan.getUnfinished());		
		plan.setProcess( (int) (process*100));
		
		Step  step=stepService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(step);
		
		step.setIsFinished(1);
		
		stepService.update(step);
		
		return "toPlanShow";	

	}
	
	public String addUI() throws Exception {

		Plan plan = planService.getById(planId);
		ActionContext.getContext().put("topic", plan);
		return "addUI";

	}
	public String add() throws Exception {
		Plan plan=planService.getById(planId);
		plan.setUnfinished(plan.getUnfinished()+1);
		
		Double process= (double)plan.getFinished()/(double)(plan.getFinished()+plan.getUnfinished());		
		plan.setProcess( (int) (process*100));
		
		// 封装
		// >> 表单字段，已经封装了title, content
		// model.setTitle(title);
		// model.setContent(content);
		model.setPlan(planService.getById(planId));
		model.setIsFinished(0);
		// >> 当前信息

		// 保存
		stepService.save(model);
		return "toPlanShow";
	}

	public Long getPlanId() {
		return planId;
	}

	public void setPlanId(Long planId) {
		this.planId = planId;
	}
	
}
